﻿using System;
using UniRx;
using UnityEngine;

/// <summary>
/// 怪物实体
/// </summary>
public class MonsterEntity : UnitEntityBase
{
    /// <summary>
    /// 所属的刷新点
    /// </summary>
    public MonsterSpawner spawner { get; private set; }
    public void SignSpawner(MonsterSpawner spawner)
    {
        this.spawner = spawner;
    }


    /// <summary>
    /// 怪物配置
    /// </summary>
    public MonsterConfig config => spawner.monsterConfig;

    /// <summary>
    /// 怪物配置
    /// </summary>
    public MonsterAttributeConfig attributeConfig => spawner.monsterAttributeConfig;


    /// <summary>
    /// 仇恨对象
    /// </summary>
    public UnitEntityBase hatred { get { return m_Hatred.Value; } set { m_Hatred.Value = value; } }
    private ReactiveProperty<UnitEntityBase> m_Hatred = new ReactiveProperty<UnitEntityBase>();
    public IObservable<UnitEntityBase> onHatredChanged => m_Hatred;


    protected override UnitControllerBase GetController()
    {
        switch (config.behaviorType)
        {
            case MonsterBehaviorType.Walking:
                return new MonsterWalkingController();

            default:
                Debug.LogError($"实体控制器生成失败： {config.behaviorType} 控制器未实现");
                return null;
        }
    }

    protected override FSMBase GetFSM()
    {
        switch (config.behaviorType)
        {
            case MonsterBehaviorType.Walking:
                return new WalkMonsterFSM();

            default:
                Debug.LogError($"实体状态机生成失败： {config.behaviorType} 状态机未实现");
                return null;
        }
    }

    protected override UnitAttributeBase GetAttribute() => new MonsterAttribute();

    protected override SkillManagerBase GetSkillManager() => new MonsterSkillManager();

    protected override void OnSpawn()
    {
        base.OnSpawn();
        hatred = null;
    }

    protected override void OnDespawn()
    {
        spawner.Despawn(this);
        base.OnDespawn();
    }
}